import wx from 'weixin-js-sdk'
import axios from 'axios'
import { shareLink, shareTitle, shareDesc, shareImg } from './weChatShare_conf'

export function H5WeChat_init(obj) {
  // const url = encodeURIComponent(window.location.href.split('#')[0])
  const realUrl = encodeURIComponent(window.location.href)
  const appid = 'wx26419932e8cf3121'
  axios({
    url: `https://dxapi.hntv.tv/dxshare/wxconfig?url=${realUrl}&appid=${appid}`,
    method: 'get',
    headers: {
      'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
    }
  })
    .then(res => {
      let { code, data } = res.data
      if (code == 0) {
        wx.config({
          debug: false,
          appId: data.appId,
          timestamp: data.timestamp,
          nonceStr: data.nonceStr,
          signature: data.signature,
          jsApiList: [
            'updateAppMessageShareData',
            'updateTimelineShareData',
            'onMenuShareWeibo',
            'onMenuShareQZone',
            'chooseImage',
            'previewImage',
            'getNetworkType',
            'openLocation',
            'getLocation',
            'hideOptionMenu',
            'showOptionMenu',
            'hideMenuItems',
            'showMenuItems',
            'hideAllNonBaseMenuItem',
            'showAllNonBaseMenuItem',
            'scanQRCode',
            'chooseWXPay',
            'openProductSpecificView',
            'checkJsApi',
            'onMenuShareQQ',
            'onMenuShareTimeline',
            'onMenuShareAppMessage'
          ],
          openTagList: ['wx-open-launch-app', 'wx-open-launch-weapp']
        })
      }
    })
    .catch(err => {
      console.log(err)
    })

  wx.ready(() => {
    wx.onMenuShareTimeline({
      title: obj.shareTitle || shareTitle,
      desc: obj.shareDesc || shareDesc,
      link: obj.shareLink || shareLink,
      imgUrl: obj.shareImg || shareImg,
      success: res => {
        // 分享朋友圈
        console.log(res)
      },
      cancel: () => {}
    })

    wx.onMenuShareAppMessage({
      title: obj.shareTitle || shareTitle,
      desc: obj.shareDesc || shareDesc,
      link: obj.shareLink || shareLink,
      imgUrl: obj.shareImg || shareImg,
      success: res => {
        // 分享好友
        console.log(res, 'onMenuShareAppMessage')
      },
      cancel: function () {}
    })
    wx.onMenuShareWeibo({
      title: obj.shareTitle || shareTitle,
      desc: obj.shareDesc || shareDesc,
      link: obj.shareLink || shareLink,
      imgUrl: obj.shareImg || shareImg,
      success: res => {
        console.log(res, 'onMenuShareWeibo')
      },
      cancel: () => {}
    })

    wx.onMenuShareQZone({
      title: obj.shareTitle || shareTitle,
      desc: obj.shareDesc || shareDesc,
      link: obj.shareLink || shareLink,
      imgUrl: obj.shareImg || shareImg,
      success: res => {
        // QQ空间
        console.log(res)
      },
      cancel: () => {}
    }),
      wx.onMenuShareQQ({
        title: obj.shareTitle || shareTitle,
        desc: obj.shareDesc || shareDesc,
        link: obj.shareLink || shareLink,
        imgUrl: obj.shareImg || shareImg,
        success: res => {
          // 分享QQ
          console.log(res)
        },
        cancel: () => {}
      })
  })
}
